package itsense.client;
import itsense.model.MemberDTO;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler; 
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginPanel extends Window{
	
	BeautySenseServiceAsync service = null;
	DynamicForm form = new DynamicForm();
	DynamicForm form2 = new DynamicForm();

	static LoginPanel instance;
	IButton loginButton = new IButton("Log in");
	IButton infoButton = new IButton();
	
	public LoginPanel(BeautySenseServiceAsync service) {
		instance = this;
		this.service = service;
		init();
	}
	
	
	
	
	private void init(){
		setWidth(360);  
		setHeight(190);  
		setShowCloseButton(false);
		setTitle("BeautySense - IT Sense");  
		setShowMinimizeButton(false);
		setIsModal(true);  
		setShowModalMask(true);  
		centerInPage();  
		setCanDragReposition(false);
		infoButton.setIcon("help-icon.png");
		   
		
		final Img keyImage = new Img("key-icon.png", 48, 48);
		
		TextItem login = new TextItem();  
		login.setName("login");
		login.setTitle("User login");
		login.setRequired(true);
		
		PasswordItem password = new PasswordItem();  
		password.setName("password");
		password.setTitle("Password");  
		password.setRequired(true);  

		LinkItem newUserItem = new LinkItem();
		newUserItem.setName("newUserItem");
		newUserItem.setTitle("New user");
		newUserItem.setLinkTitle("create account ...");
		
		
		LinkItem forgetPassword = new LinkItem();
		forgetPassword.setName("forgetPassword");
		forgetPassword.setTitle("Forget password");
		forgetPassword.setLinkTitle("remeber me ...");
		form2.setFields(newUserItem,forgetPassword);
		form.setFields(login,password);
		
		newUserItem.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
			public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
				
			}
		});
		

		
		loginButton.addClickHandler(new ClickHandler() {  
			public void onClick(ClickEvent event) {
				if( form.validate() == false) return;
				service.login(form.getValueAsString("login"), form.getValueAsString("password"), new AsyncCallback<MemberDTO>() {
					
					public void onSuccess(MemberDTO result) {
						if (result == null){
							SC.warn("Login or password is not correct! Please provide valid data.");
							form.setValue("login", "");
							form.setValue("password","");
						}else{
							BeautySenseGWT.member = result;
							BeautySenseGWT.hideLoginPanel();
							BeautySenseGWT.buildUI();
						}
					}
					
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
				//LoginPanel.instance.destroy();	
				//RootPanel.get().add(new AdminPanel(service));
			}
		});  

		form.setTop(20);
		form.setLeft(40);
		loginButton.setTop(80);
		loginButton.setLeft(155);
		form2.setWidth(200);
		form2.setTop(110);
		form2.setLeft(6);
		keyImage.setTop(20);
		keyImage.setLeft(260);
		
		VLayout vLayout = new VLayout();
		vLayout.addChild(form);
		vLayout.addChild(loginButton);
		vLayout.addChild(form2);
		vLayout.addChild(keyImage);
		
		addItem(vLayout);
		
	
	}
}
